<template>
  <div class="profile-container">
    <div class="profile-header">
      <h1>个人信息</h1>
      <button
        class="edit-btn"
        @click="toggleEdit"
        :disabled="loading"
      >
        {{ isEditing ? '保存' : '编辑' }}
      </button>
    </div>

    <div class="profile-content" v-loading="loading">
      <!-- 姓名行 -->
      <div class="profile-row">
        <div class="row-label">姓名:</div>
        <div class="row-value" v-if="!isEditing">
          {{ profileData.name }}
        </div>
        <el-input
          v-else
          v-model="profileData.name"
          class="row-input"
          placeholder="请输入姓名"
          clearable
        />
      </div>

      <!-- 身份证号行 -->
      <div class="profile-row">
        <div class="row-label">身份证号:</div>
        <div class="row-value" v-if="!isEditing">
          {{ profileData.idCard }}
        </div>
        <el-input
          v-else
          v-model="profileData.idCard"
          class="row-input"
          placeholder="请输入身份证号"
          clearable
        />
      </div>

      <!-- 车牌号行 -->
      <div class="profile-row">
        <div class="row-label">车牌号:</div>
        <div class="row-value" v-if="!isEditing">
          {{ profileData.licensePlate }}
        </div>
        <el-input
          v-else
          v-model="profileData.licensePlate"
          class="row-input"
          placeholder="请输入车牌号"
          clearable
        />
      </div>

      <!-- 联系电话行 -->
      <div class="profile-row">
        <div class="row-label">联系电话:</div>
        <div class="row-value" v-if="!isEditing">
          {{ profileData.telephone }}
        </div>
        <el-input
          v-else
          v-model="profileData.telephone"
          class="row-input"
          placeholder="请输入联系电话"
          clearable
        />
      </div>

      <!-- 公司行 -->
      <div class="profile-row">
        <div class="row-label">公司:</div>
        <div class="row-value" v-if="!isEditing">
          {{ profileData.company }}
        </div>
        <el-input
          v-else
          v-model="profileData.company"
          class="row-input"
          placeholder="请输入公司"
          clearable
        />
      </div>



    </div>
  </div>
</template>

<script>
import { getDriverInfoPhone, updateDriverInfoPhone } from "@/api/system/drivers";

export default {
  name: 'UserProfile',
  data() {
    return {
      isEditing: false,
      loading: false,
      profileData: {
        name: '',
        idCard: '',
        licensePlate: '',
        telephone: '',
        company: ''
      },
      originalData: {}
    }
  },
  created() {
    this.fetchProfileData()
  },
  methods: {
    // 获取用户信息
    async fetchProfileData() {
      this.loading = true;
      try {
        // 调用若依框架的API接口
        const { data } = await getDriverInfoPhone();
        this.profileData = data;
      } catch (error) {
        console.error('获取用户信息失败:', error);
        this.$message.error('获取用户信息失败');
      } finally {
        this.loading = false;
      }
    },

    toggleEdit() {
      if (this.isEditing) {
        // 保存操作
        this.saveProfile();
      } else {
        // 进入编辑模式，保存原始数据以便取消时恢复
        this.originalData = { ...this.profileData };
      }
      this.isEditing = !this.isEditing;
    },

    // 保存用户信息
    async saveProfile() {
      this.loading = true;
      try {
        // 调用若依框架的API接口保存数据
        await updateDriverInfoPhone(this.profileData);
        this.$message.success('保存成功！');
      } catch (error) {
        console.error('保存用户信息失败:', error);
        this.$message.error('保存失败，请重试');
      } finally {
        this.loading = false;
      }
    }
  }
}
</script>

<style scoped>
.profile-container {
  max-width: 100%;
  padding: 20px;
  box-sizing: border-box;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
}

.profile-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 30px;
  padding-bottom: 15px;
  border-bottom: 1px solid #eee;
}

.profile-header h1 {
  margin: 0;
  font-size: 24px;
  font-weight: 600;
  color: #333;
}

.edit-btn {
  padding: 8px 16px;
  background-color: #409eff;
  color: white;
  border: none;
  border-radius: 4px;
  font-size: 14px;
  cursor: pointer;
  transition: background-color 0.3s;
}

.edit-btn:hover {
  background-color: #337ecc;
}

.edit-btn:disabled {
  background-color: #a0cfff;
  cursor: not-allowed;
}

.profile-content {
  display: flex;
  flex-direction: column;
  gap: 15px;
}

.profile-row {
  display: flex;
  align-items: center;
  padding: 12px 0;
  border-bottom: 1px solid #f0f0f0;
}

.row-label {
  font-size: 14px;
  color: #666;
  width: 80px;
  margin-right: 15px;
  flex-shrink: 0;
}

.row-value {
  font-size: 16px;
  color: #333;
  flex: 1;
  word-break: break-all;
}

.row-input {
  flex: 1;
}

.row-input >>> .el-input__inner {
  font-size: 16px;
}

/* 移动端适配 */
@media (max-width: 768px) {
  .profile-container {
    padding: 15px;
  }

  .profile-header h1 {
    font-size: 20px;
  }

  .edit-btn {
    padding: 6px 12px;
    font-size: 14px;
  }

  .row-label {
    font-size: 13px;
    width: 70px;
  }

  .row-value,
  .row-input >>> .el-input__inner {
    font-size: 15px;
  }
}

@media (max-width: 480px) {
  .profile-container {
    padding: 10px;
  }

  .profile-header {
    margin-bottom: 20px;
  }

  .profile-row {
    padding: 10px 0;
  }

  .row-label {
    width: 60px;
    margin-right: 10px;
  }
}
</style>
